{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Projection Area of 3D Shapes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #geometry #array #math #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #几何 #数组 #数学 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: projectionArea"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #三维形体投影面积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>在<meta charset=\"UTF-8\" />&nbsp;<code>n x n</code>&nbsp;的网格<meta charset=\"UTF-8\" />&nbsp;<code>grid</code>&nbsp;中，我们放置了一些与 x，y，z 三轴对齐的<meta charset=\"UTF-8\" />&nbsp;<code>1 x 1 x 1</code>&nbsp;立方体。</p>\n",
    "\n",
    "<p>每个值&nbsp;<code>v = grid[i][j]</code>&nbsp;表示 <code>v</code>&nbsp;个正方体叠放在单元格&nbsp;<code>(i, j)</code>&nbsp;上。</p>\n",
    "\n",
    "<p>现在，我们查看这些立方体在 <code>xy</code>&nbsp;、<code>yz</code>&nbsp;和 <code>zx</code>&nbsp;平面上的<em>投影</em>。</p>\n",
    "\n",
    "<p><strong>投影</strong>&nbsp;就像影子，将 <strong>三维</strong> 形体映射到一个 <strong>二维</strong> 平面上。从顶部、前面和侧面看立方体时，我们会看到“影子”。</p>\n",
    "\n",
    "<p>返回 <em>所有三个投影的总面积</em> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<ul>\n",
    "</ul>\n",
    "\n",
    "<ul>\n",
    "</ul>\n",
    "\n",
    "<ul>\n",
    "</ul>\n",
    "\n",
    "<ul>\n",
    "</ul>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/02/shadow.png\" style=\"height: 214px; width: 800px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>[[1,2],[3,4]]\n",
    "<strong>输出：</strong>17\n",
    "<strong>解释：</strong>这里有该形体在三个轴对齐平面上的三个投影(“阴影部分”)。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例&nbsp;2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>grid = [[2]]\n",
    "<strong>输出：</strong>5\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>[[1,0],[0,2]]\n",
    "<strong>输出：</strong>8\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == grid.length == grid[i].length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 50</code></li>\n",
    "\t<li><code>0 &lt;= grid[i][j] &lt;= 50</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [projection-area-of-3d-shapes](https://leetcode.cn/problems/projection-area-of-3d-shapes/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [projection-area-of-3d-shapes](https://leetcode.cn/problems/projection-area-of-3d-shapes/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2],[3,4]]', '[[2]]', '[[1,0],[0,2]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        rows, cols = grid.__len__(), grid[0].__len__()\n",
    "        ans = 0\n",
    "        # 俯视图 元素>0 即有面积；侧视图为每行最大\n",
    "        for i in range(rows):\n",
    "            row_max = 0\n",
    "            for j in range(cols):\n",
    "                if grid[i][j] > 0:\n",
    "                    ans += 1\n",
    "                row_max = max(row_max, grid[i][j])\n",
    "            ans += row_max\n",
    "        # 正视图为每列最大\n",
    "        for i in range(cols):\n",
    "            col_max = 0\n",
    "            for j in range(rows):\n",
    "                col_max = max(col_max, grid[j][i])\n",
    "            ans += col_max\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        xy = sum(v > 0 for row in grid for v in row)\n",
    "        yz = sum(map(max, zip(*grid)))\n",
    "        zx = sum(map(max, grid))\n",
    "        return xy + yz + zx\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        sum = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            sum += max(grid[i][:])\n",
    "            sum += max(grid[j][i] for j in range(n))\n",
    "            for j in range(n):\n",
    "                if grid[i][j] > 0:\n",
    "                    sum += 1\n",
    "\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        sum = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            sum += max(grid[i][:])\n",
    "            sum += max(grid[j][i] for j in range(n))\n",
    "            for j in range(n):\n",
    "                if grid[i][j] > 0:\n",
    "                    sum += 1\n",
    "\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        return sum([1 if grid[i][j]!=0 else 0 for i in range(n) for j in range(n)])+sum([max(grid[i]) for i in range(n)])+sum([max([grid[i][j] for i in range(n)]) for j in range(n)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        area_x, area_y, area_z = [0]*m, [0]*n, 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                area_z += 1 if grid[i][j] else 0\n",
    "                area_x[i] = max(area_x[i], grid[i][j])\n",
    "                area_y[j] = max(area_y[j], grid[i][j])\n",
    "        return sum(area_x+area_y) + area_z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        temp1 = 0\n",
    "        for i in grid:\n",
    "            temp1 += max(i)\n",
    "        for j in range(len(grid[0])):\n",
    "            temp = -float(\"inf\")\n",
    "            for i in range(len(grid)):\n",
    "                if grid[i][j] != 0:\n",
    "                    temp1 += 1\n",
    "                temp = max(temp,grid[i][j])\n",
    "            temp1 += temp\n",
    "        return temp1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        output = sum(v > 0 for row in grid for v in row)\n",
    "        output += sum(map(max, zip(*grid)))\n",
    "        output += sum(map(max, grid))\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        output = sum(v > 0 for row in grid for v in row)\n",
    "        output += sum(map(max, zip(*grid)))\n",
    "        output += sum(map(max, grid))\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        count_xyz = {'xy':set(), 'xz':set(), 'yz':set()}\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] > 0:\n",
    "                    count_xyz['xy'].add((i,j))\n",
    "                    for k in range(grid[i][j]):\n",
    "                        count_xyz['xz'].add((i,k))\n",
    "                        count_xyz['yz'].add((j,k))\n",
    "        return len(count_xyz['xy']) + len(count_xyz['xz']) + len(count_xyz['yz'])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        sumX,sumY,sumZ = 0,0,0\n",
    "\n",
    "        for i in range(len(grid)):\n",
    "            a,b = 0,0\n",
    "            for j in range(len(grid[i])):\n",
    "                if grid[i][j] !=0:\n",
    "                    sumX+=1\n",
    "                a = max(a,grid[i][j])\n",
    "                b = max(b,grid[j][i])\n",
    "            sumY+=a\n",
    "            sumZ+=b\n",
    "        \n",
    "        return sumX+sumY+sumZ\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(grid)):\n",
    "            max_v=0\n",
    "            max_v2=0\n",
    "            for j in range(len(grid[i])):\n",
    "                if grid[i][j]!=0:\n",
    "                    ans+=1\n",
    "                max_v=max(grid[i][j],max_v)\n",
    "                max_v2=max(grid[j][i],max_v2)\n",
    "            ans += max_v\n",
    "            ans +=max_v2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        xy = sum(v > 0 for row in grid for v in row)\n",
    "        yz = sum(map(max, zip(*grid)))\n",
    "        zx = sum(map(max, grid))\n",
    "        return xy + yz + zx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        top = sum(map(bool, chain.from_iterable(grid)))\n",
    "        side = sum(map(max, grid))\n",
    "        front = sum(map(max, zip(*grid)))\n",
    "        return top + side + front"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        x=0\n",
    "        y=0\n",
    "        r=[0]*50\n",
    "        for i in range(len(grid)):\n",
    "            y+=max(grid[i])\n",
    "            for l in range(len(grid[i])):\n",
    "                if grid[i][l]!=0:\n",
    "                    x+=1\n",
    "                if grid[i][l]>r[l]: \n",
    "                    r[l]=grid[i][l]\n",
    "        #print(x)\n",
    "        #print(y)\n",
    "        return x+y+sum(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a=[0]*len(grid)\n",
    "        b=[0]*len(grid)\n",
    "        sum_area=0\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid)):\n",
    "                a[i]=max(grid[i][j],a[i])\n",
    "                b[i]=max(grid[j][i],b[i])\n",
    "                if grid[i][j]:\n",
    "                    sum_area+=1\n",
    "            sum_area+=(a[i]+b[i])\n",
    "        return sum_area\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        sum = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            sum += max(grid[i][:])\n",
    "            sum += max(grid[j][i] for j in range(n))\n",
    "            for j in range(n):\n",
    "                if grid[i][j] > 0:\n",
    "                    sum += 1\n",
    "\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        # m, n = len(grid), len(grid[0])\n",
    "        # area_x, area_y, area_z = [0]*m, [0]*n, 0\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         area_z += 1 if grid[i][j] else 0\n",
    "        #         area_x[i] = max(area_x[i], grid[i][j])\n",
    "        #         area_y[j] = max(area_y[j], grid[i][j])\n",
    "        # return sum(area_x+area_y) + area_z\n",
    "\n",
    "        # ---------------------------------------\n",
    "        return len(grid)**2 - sum(grid, []).count(0) + sum(map(max, grid)) + sum(map(max, zip(*grid)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        top, front, left = 0, 0, 0\n",
    "        n = len(grid)\n",
    "        \n",
    "        top = sum(v > 0 for row in grid for v in row)\n",
    "\n",
    "        front = sum(max(row) for row in grid)\n",
    "\n",
    "        left = sum(max(grid[i][j] for i in range(n)) for j in range(n))\n",
    "        return top + front + left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        output = sum(v > 0 for row in grid for v in row)\n",
    "        output += sum(map(max, zip(*grid)))\n",
    "        output += sum(map(max, grid))\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "\n",
    "        # v = grid[i][j]\n",
    "\n",
    "        \n",
    "        m, n = len(grid), len(grid[0])\n",
    "        top, frontTotal, leftTotal = 0, 0, 0\n",
    "\n",
    "        front = [0] * n\n",
    "        left = [0] * m\n",
    "\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                v = grid[i][j]\n",
    "\n",
    "                if v != 0:\n",
    "                    top += 1\n",
    "                    left[i] = max(left[i], v)\n",
    "                    front[j] = max(front[j], v)\n",
    "        \n",
    "        return top + sum(left) + sum(front)\n",
    "\n",
    "                \n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        l = len(grid)\n",
    "        ans = l**2\n",
    "        for i in range(l):\n",
    "            ans += max(grid[i])\n",
    "            tmp = 0\n",
    "            j = 0\n",
    "            while j < l:\n",
    "                if grid[j][i] > tmp:\n",
    "                    tmp = grid[j][i]\n",
    "                if grid[j][i] == 0:\n",
    "                    ans -= 1\n",
    "                j += 1\n",
    "            ans += tmp\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        xyArea = sum(v > 0 for row in grid for v in row)\n",
    "        yzArea = sum(map(max, zip(*grid)))\n",
    "        zxArea = sum(map(max, grid))\n",
    "        return xyArea + yzArea + zxArea"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        r,c = len(grid),len(grid[0])\n",
    "        for i in range(r):\n",
    "            t = 0\n",
    "            for j in range(c):\n",
    "                t = max(t,grid[j][i])\n",
    "                ans += (grid[i][j] != 0)\n",
    "            ans += t\n",
    "            ans += max(grid[i])\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        lenth=len(grid)\n",
    "        xy=sum([sum([1 if grid[i][j] != 0 else 0 for i in range(lenth)]) for j in range(lenth)])\n",
    "        xz=sum([max([grid[i][j] for j in range(lenth)]) for i in range(lenth)])\n",
    "        yz=sum([max([grid[i][j] for i in range(lenth)]) for j in range(lenth)])\n",
    "        return xy+xz+yz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        output = sum(v > 0 for row in grid for v in row)\n",
    "        output += sum(map(max, zip(*grid)))\n",
    "        output += sum(map(max, grid))\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n1,n2,n3 = 0,0,0\n",
    "        r,c = len(grid),len(grid[0])\n",
    "        for i in range(r):\n",
    "            t = 0\n",
    "            for j in range(c):\n",
    "                t = max(t,grid[j][i])\n",
    "                n1 += (grid[i][j] != 0)\n",
    "            n3 += t\n",
    "            n2 += max(grid[i])\n",
    "        return n1 + n2 + n3\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a = sum([1 for i in grid for j in i if j > 0])\n",
    "        b = sum([max(i) for i in grid])\n",
    "        c = sum([max(i) for i in zip(*grid)])\n",
    "        return a + b + c\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            maxx = 0\n",
    "            maxy =0\n",
    "            for k in range(n):\n",
    "                if grid[i][k] != 0 :\n",
    "                    res +=1\n",
    "                maxx = max(grid[i][k],maxx)\n",
    "                maxy = max(grid[k][i],maxy)\n",
    "            res += maxx\n",
    "            res += maxy\n",
    "        return res\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        output = sum(v > 0 for row in grid for v in row)\n",
    "        output += sum(map(max, zip(*grid)))\n",
    "        output += sum(map(max, grid))\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        ans=0\n",
    "        m=len(grid)\n",
    "        i,j=0,0\n",
    "        l=[[0]*m for i in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(m):\n",
    "                l[i][j]=grid[j][i]\n",
    "                if grid[i][j]!=0:\n",
    "                    ans=ans+1\n",
    "            ans=ans+max(grid[i])\n",
    "        for i in range(m):\n",
    "            ans=ans+max(l[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        \n",
    "        \n",
    "        \n",
    "        # my solution ... \n",
    "        \n",
    "        n = len(grid)\n",
    "        res = 0\n",
    "        res += sum(1 if v else 0 for row in grid for v in row)\n",
    "        res += sum(max(row) for row in grid)\n",
    "        res += sum(max(col) for col in zip(*grid))\n",
    "        return res\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        return sum(map(max, grid + list(zip(*grid)))) + sum(v > 0 for row in grid for v in row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        output = sum(v > 0 for row in grid for v in row)\n",
    "        output += sum(map(max, zip(*grid)))\n",
    "        output += sum(map(max, grid))\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a1, b1, c1 = 0, 0, 0\n",
    "        \n",
    "        c_list = [[] for _ in range(len(grid))]\n",
    "        for g in grid:\n",
    "            for i in range(len(g)):\n",
    "                if g[i] > 0:\n",
    "                    a1 += 1\n",
    "                c_list[i].append(g[i])\n",
    "        for b in grid:\n",
    "            b1+=max(b)\n",
    "        for c in c_list:\n",
    "            c1+=max(c)\n",
    "        return a1+b1+c1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(grid)):\n",
    "            max_v=0\n",
    "            max_v2=0\n",
    "            for j in range(len(grid[i])):\n",
    "                if grid[i][j]!=0:\n",
    "                    ans+=1\n",
    "                max_v=max(grid[i][j],max_v)\n",
    "                max_v2=max(grid[j][i],max_v2)\n",
    "            ans += max_v\n",
    "            ans +=max_v2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a=len(grid)**2\n",
    "        for i in range(len(grid)):\n",
    "            a-=grid[i].count(0)\n",
    "            a+=max(grid[i])\n",
    "        for i in range(len(grid)):\n",
    "            b=0\n",
    "            for j in range(len(grid)):\n",
    "                b=max(b,grid[j][i])\n",
    "            a+=b\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        row, col = len(grid), len(grid[0])\n",
    "        area_xy, area_yz, area_zx = 0, 0, 0\n",
    "        row_max, col_max = [0]*row, [0]*col\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if grid[i][j] != 0:\n",
    "                    area_xy += 1\n",
    "                if grid[i][j] > row_max[i]:\n",
    "                    row_max[i] = grid[i][j]\n",
    "                if grid[i][j] > col_max[j]:\n",
    "                    col_max[j] = grid[i][j]\n",
    "        area_yz, area_zx =  sum(col_max), sum(row_max)\n",
    "        return area_xy+area_yz+area_zx\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        xyArea = sum(v > 0 for row in grid for v in row)\n",
    "        yzArea = sum(map(max, zip(*grid)))  # 注意这里为 O(n) 空间复杂度，改为下标枚举则可以 O(1)\n",
    "        zxArea = sum(map(max, grid))\n",
    "        return xyArea + yzArea + zxArea"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        xy = sum(x > 0 for row in grid for x in row)\n",
    "        yz = sum(map(max, zip(*grid)))\n",
    "        zx = sum(map(max, grid))\n",
    "        return xy + yz + zx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        output = sum(v > 0 for row in grid for v in row)\n",
    "        output += sum(map(max, zip(*grid)))\n",
    "        output += sum(map(max, grid))\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        x,c,d,nums3=[],[],[],0\n",
    "        def mul(a):\n",
    "            for i in a:\n",
    "                a=max(i)\n",
    "                x.append(a)\n",
    "            return sum(x)\n",
    "        for b in range(len(grid[0])):\n",
    "            for i in range(len(grid)):\n",
    "                c.append(grid[i][b])\n",
    "                if grid[i][b]!=0:\n",
    "                    nums3+=1\n",
    "            d.append(c)\n",
    "            c=[]\n",
    "        nums1=mul(d)\n",
    "        nums2=mul(grid)\n",
    "        return nums2+nums3\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        xyArea = sum(v > 0 for row in grid for v in row)\r\n",
    "        yzArea = sum(map(max, zip(*grid)))  # 注意这里为 O(n) 空间复杂度，改为下标枚举则可以 O(1)\r\n",
    "        zxArea = sum(map(max, grid))\r\n",
    "        return xyArea + yzArea + zxArea\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        bottom = sum(len(list(filter(None, _))) for _ in grid)\n",
    "        x = sum(max(_) for _ in grid)\n",
    "        y = sum(max(_) for _ in zip(*grid))\n",
    "        return bottom + x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        w=len(grid)\n",
    "        d=len(grid[0])\n",
    "        xz=[0]*w\n",
    "        yz=[0]*d\n",
    "        result=0\n",
    "        for x in range(w):\n",
    "            for y in range(d):\n",
    "                if grid[x][y]>0:\n",
    "                    result+=1\n",
    "                    xz[x]=max(xz[x],grid[x][y])\n",
    "                    yz[y]=max(yz[y],grid[x][y])\n",
    "        return result+sum(xz)+sum(yz)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        zxhashmap = {}\n",
    "        zyhashmap = {}\n",
    "        xyset = set()\n",
    "        res = 0\n",
    "        for x in range(len(grid)):\n",
    "            for y in range(len(grid[0])):\n",
    "                if grid[x][y] != 0:\n",
    "                    res += 1\n",
    "                if x in zxhashmap:\n",
    "                    zxhashmap[x] = max(zxhashmap[x],grid[x][y])\n",
    "                else:\n",
    "                    zxhashmap[x] = grid[x][y]\n",
    "                if y in zyhashmap:\n",
    "                    zyhashmap[y] = max(zyhashmap[y],grid[x][y])\n",
    "                else:\n",
    "                    zyhashmap[y] = grid[x][y]\n",
    "        for x in zxhashmap.keys():\n",
    "            res += zxhashmap[x]\n",
    "        for y in zyhashmap.keys():\n",
    "            res += zyhashmap[y]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        grid_T = list(zip(*grid))\n",
    "        for i in range(len(grid)):\n",
    "            res += max(grid[i])\n",
    "            res += max(grid_T[i])\n",
    "            for j in range(len(grid[i])):\n",
    "                if grid[i][j] != 0:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(grid)):\n",
    "            max_v=0\n",
    "            max_v2=0\n",
    "            for j in range(len(grid[i])):\n",
    "                if grid[i][j]!=0:\n",
    "                    ans+=1\n",
    "                max_v=max(grid[i][j],max_v)\n",
    "                max_v2=max(grid[j][i],max_v2)\n",
    "            ans += max_v\n",
    "            ans +=max_v2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        xy = 0\n",
    "        yz = [0] * len(grid[0])\n",
    "        xz = [0] * len(grid)\n",
    "        for i, m in enumerate(grid):\n",
    "            xz[i] = max(m)\n",
    "            for j, n in enumerate(m):\n",
    "                yz[j] = max(yz[j], n)\n",
    "                if n != 0:\n",
    "                    xy += 1\n",
    "        return xy + sum(yz) + sum(xz)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        side_list, front_list = [0 for _ in range(n)], [0 for _ in range(n)]\n",
    "        side_area, front_area, bottom_area = 0, 0, 0\n",
    "\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]:\n",
    "                    bottom_area += 1\n",
    "                    side_list[i] = max(side_list[i], grid[i][j])\n",
    "                    front_list[j] = max(front_list[j], grid[i][j])\n",
    "        return sum(side_list) + sum(front_list) + bottom_area"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "class Solution:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        m, n = len(grid), len(grid[0])\r\n",
    "        x_max = [0] * m\r\n",
    "        y_max = [0] * n\r\n",
    "        res = 0\r\n",
    "        for i in range(m):\r\n",
    "            for j in range(n):\r\n",
    "                val = grid[i][j]\r\n",
    "                if val != 0:\r\n",
    "                    res += 1\r\n",
    "                    x_max[i] = max(x_max[i], val)\r\n",
    "                    y_max[j] = max(y_max[j], val)\r\n",
    "        return res + sum(x_max) + sum(y_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        w=len(grid)\n",
    "        d=len(grid[0])\n",
    "        xz=[0]*w\n",
    "        yz=[0]*d\n",
    "        result=0\n",
    "        for x in range(w):\n",
    "            for y in range(d):\n",
    "                if grid[x][y]>0:\n",
    "                    result+=1\n",
    "                    xz[x]=max(xz[x],grid[x][y])\n",
    "                    yz[y]=max(yz[y],grid[x][y])\n",
    "        return result+sum(xz)+sum(yz)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        top = sum(map(bool, chain.from_iterable(grid)))\n",
    "        side = sum(map(max, grid))\n",
    "        front = sum(map(max, zip(*grid)))\n",
    "        return top + side + front"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        ret=0\r\n",
    "        R,C=len(grid),len(grid[0])\r\n",
    "        x_heights=[0]*C\r\n",
    "        y_heights=[0]*R\r\n",
    "        for r in range(R):\r\n",
    "            for c in range(C):\r\n",
    "                if grid[r][c]:\r\n",
    "                    ret+=1\r\n",
    "                    x_heights[c]=max(x_heights[c],grid[r][c])\r\n",
    "                    y_heights[r]=max(y_heights[r],grid[r][c])\r\n",
    "        return ret+sum(x_heights)+sum(y_heights)\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a1, b1, c1 = 0, 0, 0\n",
    "        b_list = []\n",
    "        c_list = [[] for _ in range(len(grid))]\n",
    "        for g in grid:\n",
    "            for i in range(len(g)):\n",
    "                if g[i] > 0:\n",
    "                    a1 += 1\n",
    "                c_list[i].append(g[i])\n",
    "            b_list.append(g)\n",
    "        for b in b_list:\n",
    "            b1+=max(b)\n",
    "        for c in c_list:\n",
    "            c1+=max(c)\n",
    "        return a1+b1+c1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        bottom, front, side = 0, 0, 0\n",
    "\n",
    "        # 计算底面积\n",
    "        for x in grid:\n",
    "            for y in x:\n",
    "                if y != 0:\n",
    "                    bottom += 1\n",
    "\n",
    "        # 计算正面面积\n",
    "        index_i = 0\n",
    "        while index_i < len(grid[0]):\n",
    "            index_j = 0\n",
    "            maxvalue = 0\n",
    "            while index_j < len(grid):\n",
    "                if maxvalue < grid[index_j][index_i]:\n",
    "                    maxvalue = grid[index_j][index_i]\n",
    "                index_j += 1\n",
    "            front += maxvalue\n",
    "            index_i += 1\n",
    "        \n",
    "        # 计算侧面面积\n",
    "        index_i = 0\n",
    "        while index_i < len(grid):\n",
    "            index_j = 0\n",
    "            maxvalue = 0\n",
    "            while index_j < len(grid[0]):\n",
    "                if maxvalue < grid[index_i][index_j]:\n",
    "                    maxvalue = grid[index_i][index_j]\n",
    "                index_j += 1\n",
    "            side += maxvalue\n",
    "            index_i += 1\n",
    "\n",
    "        return bottom + front + side"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "\n",
    "        # v = grid[i][j]\n",
    "\n",
    "        \n",
    "        m, n = len(grid), len(grid[0])\n",
    "        top, frontTotal, leftTotal = 0, 0, 0\n",
    "\n",
    "        front = [0] * n\n",
    "        left = [0] * m\n",
    "\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                v = grid[i][j]\n",
    "\n",
    "                if v != 0:\n",
    "                    top += 1\n",
    "                    left[i] = max(left[i], v)\n",
    "                    front[j] = max(front[j], v)\n",
    "        \n",
    "        return top + sum(left) + sum(front)\n",
    "\n",
    "                \n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a1, b1, c1 = 0, 0, 0\n",
    "        \n",
    "        c_list = [[] for _ in range(len(grid))]\n",
    "        for g in grid:\n",
    "            for i in range(len(g)):\n",
    "                if g[i] > 0:\n",
    "                    a1 += 1\n",
    "                c_list[i].append(g[i])\n",
    "        for b in grid:\n",
    "            b1+=max(b)\n",
    "        for c in c_list:\n",
    "            c1+=max(c)\n",
    "        return a1+b1+c1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "class Solution1:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        \"\"\"\r\n",
    "        O(m*n) and O(m+n)\r\n",
    "        \"\"\"\r\n",
    "        m, n = len(grid), len(grid[0])\r\n",
    "        x_max = [0] * m\r\n",
    "        y_max = [0] * n\r\n",
    "        res = 0\r\n",
    "        for i in range(m):\r\n",
    "            for j in range(n):\r\n",
    "                val = grid[i][j]\r\n",
    "                if val != 0:\r\n",
    "                    res += 1\r\n",
    "                    x_max[i] = max(x_max[i], val)\r\n",
    "                    y_max[j] = max(y_max[j], val)\r\n",
    "        return res + sum(x_max) + sum(y_max)\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        xy = sum(v > 0 for row in grid for v in row)\r\n",
    "        yz = sum(map(max, zip(*grid)))\r\n",
    "        zx = sum(map(max, grid))\r\n",
    "        return xy +yz + zx\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        w=len(grid)\n",
    "        d=len(grid[0])\n",
    "        h=max(max(x) for x in grid)\n",
    "        result=0\n",
    "        for x in range(w):\n",
    "            for y in range(d):\n",
    "                if grid[x][y]>0:\n",
    "                    result+=1\n",
    "        for x in range(w):\n",
    "            for z in range(h):\n",
    "                for y in range(d):\n",
    "                    if grid[x][y]>z:\n",
    "                        result+=1\n",
    "                        break\n",
    "        for y in range(d):\n",
    "            for z in range(h):\n",
    "                for x in range(w):         \n",
    "                    if grid[x][y]>z:\n",
    "                        result+=1\n",
    "                        break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        w=len(grid)\n",
    "        d=len(grid[0])\n",
    "        h=max(max(x) for x in grid)\n",
    "        result=0\n",
    "        for x in range(w):\n",
    "            for y in range(d):\n",
    "                if grid[x][y]>0:\n",
    "                    result+=1\n",
    "        for x in range(w):\n",
    "            for z in range(h):\n",
    "                for y in range(d):\n",
    "                    if grid[x][y]>z:\n",
    "                        result+=1\n",
    "                        break\n",
    "        for y in range(d):\n",
    "            for z in range(h):\n",
    "                for x in range(w):         \n",
    "                    if grid[x][y]>z:\n",
    "                        result+=1\n",
    "                        break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "class Solution:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        \"\"\"\r\n",
    "        O(m*n) and O(m+n)\r\n",
    "        \"\"\"\r\n",
    "        m, n = len(grid), len(grid[0])\r\n",
    "        x_max = [0] * m\r\n",
    "        y_max = [0] * n\r\n",
    "        res = 0\r\n",
    "        for i in range(m):\r\n",
    "            for j in range(n):\r\n",
    "                val = grid[i][j]\r\n",
    "                if val != 0:\r\n",
    "                    res += 1\r\n",
    "                    x_max[i] = max(x_max[i], val)\r\n",
    "                    y_max[j] = max(y_max[j], val)\r\n",
    "        return res + sum(x_max) + sum(y_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        return len(grid) ** 2 - sum(grid, []).count(0) + sum(map(max, grid)) + sum(map(max, zip(*grid)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        s=0\n",
    "        for i in range(0,len(grid)):\n",
    "            a=0\n",
    "            for j in range(0,len(grid[i])):\n",
    "                a=max(a,grid[i][j])\n",
    "            s+=a\n",
    "        t=0\n",
    "        for i in range(0,len(grid)):\n",
    "            for j in range(0,len(grid[i])):\n",
    "                t=max(t,len(grid[i]))\n",
    "        for i in range(0,len(grid)):\n",
    "            for j in range(0,len(grid[i])):\n",
    "                grid[i]=grid[i]+[0]*(t-len(grid[i]))\n",
    "        for i in range(0,t):\n",
    "            b=0\n",
    "            for j in range(0,len(grid)):\n",
    "                b=max(b,grid[j][i])\n",
    "            s+=b\n",
    "        for i in range(0,len(grid)):\n",
    "            for j in range(0,len(grid[i])):\n",
    "                if grid[i][j]!=0:\n",
    "                    s+=1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\r\n",
    "        ans = 0\r\n",
    "        left = [0] * len(grid[0]) # 左\r\n",
    "        for i in grid:\r\n",
    "            ans += max(i) # 主\r\n",
    "            for j in range(len(i)):\r\n",
    "                ans += i[j] > 0 # 上\r\n",
    "                if i[j] > left[j]:\r\n",
    "                    left[j] = i[j]\r\n",
    "        return ans + sum(left)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        xyArea = sum(v > 0 for row in grid for v in row)\n",
    "        yzArea = sum(map(max, zip(*grid)))  # 注意这里为 O(n) 空间复杂度，改为下标枚举则可以 O(1)\n",
    "        zxArea = sum(map(max, grid))\n",
    "        return xyArea + yzArea + zxArea"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a, b, c, ln = 0, 0, 0, 0\n",
    "        for row in grid:\n",
    "            ln = max(ln, len(row))\n",
    "            b += max(row)\n",
    "        lst = [[] for _ in range(ln)]\n",
    "        for row in grid:\n",
    "            for i in range(len(row)):\n",
    "                lst[i].append(row[i])\n",
    "                if row[i] != 0:\n",
    "                    a += 1\n",
    "        for col in lst:\n",
    "            c += max(col)\n",
    "        return a + b + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a1, a2, a3 = 0, 0, 0     ##顶部、前面、侧面\n",
    "        m, n = len(grid), len(grid[0])   ###行，列\n",
    "        for i in range(n):\n",
    "            tmp = 0\n",
    "            for j in range(m):\n",
    "                if grid[i][j] != 0:\n",
    "                    a1 += 1\n",
    "                tmp = max(tmp,grid[j][i])\n",
    "            a2 += tmp\n",
    "        \n",
    "        for i in range(m):\n",
    "            a3 += max(grid[i])\n",
    "        return a1 + a2 + a3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a1, b1, c1 = 0, 0, 0\n",
    "        \n",
    "        c_list = [[] for _ in range(len(grid))]\n",
    "        for g in grid:\n",
    "            for i in range(len(g)):\n",
    "                if g[i] > 0:\n",
    "                    a1 += 1\n",
    "                c_list[i].append(g[i])\n",
    "        for b in grid:\n",
    "            b1+=max(b)\n",
    "        for c in c_list:\n",
    "            c1+=max(c)\n",
    "        return a1+b1+c1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        x = sum([max(row) for row in grid])\n",
    "        y = sum([max([grid[i][j] for i in range(n)]) for j in range(n)])\n",
    "        z = sum([sum([1 if grid[i][j] != 0 else 0 for i in range(n)]) for j in range(n)])\n",
    "        return x + y + z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        return sum(sum(g != 0 for g in row) for row in grid) + sum(max(row) for row in grid) + sum(max(grid[i][j] for i in range(m)) for j in range(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        a=len(grid)\n",
    "        xy=0\n",
    "        for i in grid:\n",
    "            for m in i:\n",
    "                if m!=0:\n",
    "                    xy+=1\n",
    "        xz=0\n",
    "        for i in range(a):\n",
    "            c=max(grid[i][m] for m in range(a))\n",
    "            xz+=c\n",
    "        yz=0\n",
    "        for i in range(a):\n",
    "            d=max(grid[m][i] for m in range(a))\n",
    "            yz+=d\n",
    "        return xy+xz+yz\n",
    "        \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        rowmax = [0]*len(grid)\n",
    "        colmax = [0]*len(grid[0])\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if grid[i][j] != 0:\n",
    "                    res += 1\n",
    "                rowmax[i] = max(rowmax[i], grid[i][j])\n",
    "                colmax[j] = max(colmax[j], grid[i][j])\n",
    "        return res+sum(rowmax)+sum(colmax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        top=0\n",
    "        left=0\n",
    "        front=0\n",
    "        rowNum = len(grid)\n",
    "        columnNum=len(grid[0])\n",
    "        for i in range(rowNum):\n",
    "            left+=max(grid[i])\n",
    "            for j in range(columnNum):\n",
    "                if grid[i][j]!=0:\n",
    "                    top+=1\n",
    "        for i in range(columnNum):\n",
    "            columnMax = 0\n",
    "            for j in range(rowNum):\n",
    "                if grid[j][i]>columnMax:\n",
    "                    columnMax=grid[j][i]\n",
    "            front+=columnMax\n",
    "        return front+left+top"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        res = 0\n",
    "        #x轴\n",
    "        for i in range(n):\n",
    "            for j in  range(n):\n",
    "                if grid[i][j]!=0:\n",
    "                    res+=1\n",
    "        #y轴\n",
    "        for i in range(n):\n",
    "            res+=max(grid[i])\n",
    "\n",
    "        #z轴\n",
    "        for i in range(n): #列的最大值\n",
    "            tmp = grid[0][i]\n",
    "            for j in range(1,n):#行\n",
    "                tmp  = max(tmp,grid[j][i])\n",
    "            res+=tmp\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def projectionArea(self, grid: List[List[int]]) -> int:\n",
    "        n = len(grid)\n",
    "        sum_0=sum(map(lambda x:x.count(0),grid))\n",
    "        sx = n*n-sum_0\n",
    "        sy = sum(map(max,grid))\n",
    "        sz = sum(map(max,zip(*grid)))\n",
    "        return sx+sy+sz"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
